package com.zood.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.zood.controller.utils.R;
import com.zood.domain.stone;
import com.zood.service.IstoneService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;

@RestController
@RequestMapping("/stones")
public class StoneController {

    @Autowired
    private IstoneService stoneService;

    @GetMapping
    public R getAll(){
        return new R(true, stoneService.list());
    }

    @PostMapping
    public R save(@RequestBody stone stone) throws IOException {
        boolean flag = stoneService.save(stone);
        return new R(flag, flag ? "添加成功^_^" : "添加失败-_-!");
    }

    @PutMapping
    public R update(@RequestBody stone stone) throws IOException {
//        if (stone.getSname().equals("123") ) throw new IOException();
        boolean flag = stoneService.modify(stone);
        return new R(flag, flag ? "修改成功^_^" : "修改失败-_-!");
    }

    @PostMapping("{id}")
    public R deleteById(@PathVariable Integer id) throws IOException{
        return new R(stoneService.deletebyId(id));
    }

    @DeleteMapping
    public R delete(@RequestBody stone stone) throws IOException{
        return new R(stoneService.delete(stone));
    }

    @GetMapping("{id}")
    public R getById(@PathVariable Integer id){
        return new R(true, stoneService.getById(id));
    }

}

















